//
//  XLPersonCenterSegmented.m
//  XLPersonCenter
//
//  Created by 宋振 on 2018/7/19.
//  Copyright © 2018年 夜幕繁华. All rights reserved.
//

#import "XLPersonCenterSegmented.h"

#import "XLPersonCenterTool.h"

@implementation XLPersonCenterSegmented
{
    //评分按钮
    UIStackView *stackView;
    
    //指示器
    UIView *_indicaterView;
    
    //按钮
    __weak UIButton *_segmentButton;
}

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        
        /**
         绘制视图显示
         */
        [self setCustomeView];
    }
    
    return self;
}

#pragma mark    -   -   - 绘制主要视图 -  -   -

/**
 绘制主要视图显示：按钮排列和指示器
 */
- (void)setCustomeView
{
    //设置stackView 和 按钮
    [self layOutStackAndButton];
    
    //添加底部灰色分割线
    [self layOutLineView];
    
    //添加指示器显示
    [self layOutIndicatorView];
}

/**
 设置stackView 和 按钮
 */
- (void)layOutStackAndButton
{
    //设置stackView 用来平分按钮
    stackView = [[UIStackView alloc] init];
    stackView.backgroundColor = [UIColor whiteColor];
    //横向排放
    stackView.axis = UILayoutConstraintAxisHorizontal;
    
    //设置视图充满修饰
    stackView.distribution = UIStackViewDistributionFillEqually;
    
    //
    stackView.translatesAutoresizingMaskIntoConstraints = NO;
    
    //添加视图
    [self addSubview:stackView];
    
    //设置约束
    [stackView.topAnchor constraintEqualToAnchor:self.topAnchor].active = YES;
    [stackView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
    [stackView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
    [stackView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
    
    //添加按钮
    NSArray *buttonName = @[@"视频",@"资料"];
    
    [buttonName enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        UIButton *tempButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        tempButton.backgroundColor = [UIColor whiteColor];
        //正常
        NSAttributedString *attributeNormal = [[NSAttributedString alloc] initWithString:(NSString *)obj attributes:@{NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:kSegmentFontSize],NSForegroundColorAttributeName:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1]}];
        
        //选择
        NSAttributedString *attributeSelect = [[NSAttributedString alloc] initWithString:(NSString *)obj attributes:@{NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:kSegmentFontSize],NSForegroundColorAttributeName:[UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:1]}];
        
        //正常状态下按钮字体颜色
        [tempButton setAttributedTitle:attributeNormal forState:(UIControlStateNormal)];
        
        //选择状态下按钮字体颜色
        [tempButton setAttributedTitle:attributeSelect forState:(UIControlStateSelected)];
        
        //标签
        tempButton.tag = 100 + idx;
        
        //默认选择
        if (idx == 0) {
            self->_segmentButton = tempButton;
            tempButton.selected = YES;
        }
        
        //事件
        [tempButton addTarget:self action:@selector(clickIndex:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [self->stackView addArrangedSubview:tempButton];
        
    }];
}

/**
 添加底部灰色分割线
 */
- (void)layOutLineView
{
    UIView *lineView = [[UIView alloc] init];
    lineView.translatesAutoresizingMaskIntoConstraints = NO;
    lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:lineView];
    
    //约束
    [lineView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
    [lineView.leadingAnchor constraintEqualToAnchor:self.leadingAnchor].active = YES;
    [lineView.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
    [lineView.heightAnchor constraintEqualToConstant:0.5].active = YES;
}

/**
 添加指示器显示
 */
- (void)layOutIndicatorView
{
    _indicaterView = [[UIView alloc] init];
    _indicaterView.backgroundColor = [UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:1];
    _indicaterView.translatesAutoresizingMaskIntoConstraints = NO;
    [self addSubview:_indicaterView];
    
    //约束
    [_indicaterView.widthAnchor constraintEqualToConstant:kIndicatorWidth].active = YES;
    [_indicaterView.heightAnchor constraintEqualToConstant:2.0].active = YES;
    [_indicaterView.bottomAnchor constraintEqualToAnchor:self.bottomAnchor].active = YES;
    [_indicaterView.centerXAnchor constraintEqualToAnchor:_segmentButton.centerXAnchor].active = YES;
}









#pragma mark    -   -   - 事件处理 -    -   -

/**
 事件处理：按钮点击事件处理
 */
- (void)clickIndex:(UIButton *)sender
{
    //更新指示器
    [self updateIndicatorViewWithButton:sender];
    
    //回调事件信息
    if (_didClickButtonHandler != nil) {
        self.didClickButtonHandler(sender, sender.tag-100);
    }
}







#pragma mark    -   -   - 其他方法或者辅助事件 -  -   -

/**
 设置切换控制器之后指示器位置
 */
- (void)setIndicatorLocationWithIndex:(NSInteger)index
{
    [stackView.arrangedSubviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        //判断是否是当前的坐标
        if (idx == index) {
            *stop = YES;
            [self updateIndicatorViewWithButton:(UIButton *)obj];
        }
    }];
}

/**
 更新指示器
 */
- (void)updateIndicatorViewWithButton:(UIButton *)sender
{
    if (sender == _segmentButton) {
        return;
    }
    
    //先把上个选择的按钮设置非选状态
    _segmentButton.selected = NO;
    
    //当前选择的按钮状态
    sender.selected = YES;
    _segmentButton = sender;
    
    //更新指示器约束
    for (NSLayoutConstraint *constraint in self.constraints) {
        
        if (constraint.firstItem == _indicaterView && constraint.firstAttribute == NSLayoutAttributeCenterX) {
            
            //关闭先前约束
            constraint.active = NO;
            [self removeConstraint:constraint];
            
            //重设约束
            [_indicaterView.centerXAnchor constraintEqualToAnchor:sender.centerXAnchor].active = YES;
            
        }
        
    }
    
    //动画过度
    [UIView animateWithDuration:0.5f animations:^{
        [self layoutIfNeeded];
    }];
    
}


#pragma mark     - - - Setter - - -

- (void)setTitles:(NSArray *)titles
{
    _titles = titles;
    if (titles.count > 0) {
        
        //设置选择名称
        for (NSInteger i = 0; i < stackView.arrangedSubviews.count; i++) {
            UIButton *button = stackView.arrangedSubviews[i];
            
            //正常
            NSAttributedString *attributeNormal = [[NSAttributedString alloc] initWithString:(NSString *)titles[i] attributes:@{NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:kSegmentFontSize],NSForegroundColorAttributeName:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1]}];
            
            //选择
            NSAttributedString *attributeSelect = [[NSAttributedString alloc] initWithString:(NSString *)titles[i] attributes:@{NSFontAttributeName:[UIFont fontWithName:kFontNamePFRegular size:kSegmentFontSize],NSForegroundColorAttributeName:[UIColor colorWithRed:17/255.0 green:17/255.0 blue:17/255.0 alpha:1]}];
            
            
            //正常状态下按钮字体颜色
            [button setAttributedTitle:attributeNormal forState:(UIControlStateNormal)];
            
            //选择状态下按钮字体颜色
            [button setAttributedTitle:attributeSelect forState:(UIControlStateSelected)];
            
        }
        
    }
}




@end
